var AjaxCrud = QualityCheck.AjaxCRUD;
var serverUrl = QualityCheck.GPUD.ServiceConfig;
var Subject = null;
$(function(){
	var usercode = $("#user-name",parent.parent.document).attr("code");
	var userkey = $("#user-name",parent.parent.document).attr("key");
	var usertype = $("#user-name",parent.parent.document).attr("usertype");
	keyCheck(usercode,usertype,userkey,function(){
		initSmPages();
		window.onresize = function () {
			initSmPages();
		};
		pageBtns();
		initGradeClass();
	});
});
function initGradeClass(){
	var userKey = $("#user-name",parent.parent.document).attr("key");
	var userRoles = $("#user-name",parent.parent.document).attr("rolecodes");
	var data = {
			cmd:"getDataRole",
			model:"license",
			userKey:userKey,
			data:{
				rolecode:userRoles
			},
			remark:"",
	};
	AjaxCrud.get2(serverUrl.ServerLoginUrl,data,function(res){
		if(res.result == 1 && res.data != undefined && res.data != ""){
			var typeOpt = "";
			var gradeOpt = "";
			var data = res.data;
			for(var i = 0; i < data.length; i++){
				var name = "";
				if(data[i].type == "0"){
					name = "幼儿园";
				}else if(data[i].type == "1"){
					name = "小学";
				}else if(data[i].type == "2"){
					name = "中学";
				}else if(data[i].type == "3"){
					name = "高中";
				}
				typeOpt += "<option value='"+data[i].type+"'>"+name+"</option>";
			}
			$("#exam-type").html(typeOpt);
			
			var gradeData = data[0].data;
			for(var i = 0; i < gradeData.length; i++){
				gradeOpt += "<option value='"+gradeData[i].grade+"' classcodes='"+gradeData[i].classcode+"' classes='"+gradeData[i].classes+"'>"+gradeData[i].grade+"年级</option>";
			}
			$("#exam-grade").html(gradeOpt);
			
			var classes = $("#exam-grade option:selected").attr("classes").split(",");
			var classcodes = $("#exam-grade option:selected").attr("classcodes").split(",");
			var classOpt = "";
			for(var j = 0; j < classes.length; j++){
				classOpt += "<div class='classcode-item'><input type='checkbox' checked='checked' value='"+classcodes[j]+"' classes='"+classes[j]+"'><span>"+classes[j]+"班</span></div>";
			}
			$("#exam-classcode").html(classOpt);
			
			$("#exam-type").unbind("change");
			$("#exam-type").change(function(){
				var gradeData = null;
				var type = $("#exam-type option:selected").val();
				var gradeOpt = "";
				if(type != ""){
					for(var i = 0; i < data.length; i++){
						if(data[i].type == type){
							gradeData = data[i].data;
						}
					}
					for(var i = 0; i < gradeData.length; i++){
						gradeOpt += "<option value='"+gradeData[i].grade+"' classcodes='"+gradeData[i].classcode+"' classes='"+gradeData[i].classes+"'>"+gradeData[i].grade+"年级</option>";
					}
				}
				$("#exam-grade").html(gradeOpt);
				
				var classOpt = "";
				var classes = $("#exam-grade option:selected").attr("classes").split(",");
				var classcodes = $("#exam-grade option:selected").attr("classcodes").split(",");
				for(var j = 0; j < classes.length; j++){
					classOpt += "<div class='classcode-item'><input type='checkbox' checked='checked' value='"+classcodes[j]+"' classes='"+classes[j]+"'><span>"+classes[j]+"班</span></div>";
				}
				$("#exam-classcode").html(classOpt);
				
				getAllCourseSubject();
			});
			
			$("#exam-grade").unbind("change");
			$("#exam-grade").change(function(){
				var classes = $("#exam-grade option:selected").attr("classes").split(",");
				var classcodes = $("#exam-grade option:selected").attr("classcodes").split(",");
				var classOpt = "";
				for(var j = 0; j < classes.length; j++){
					classOpt += "<div class='classcode-item'><input type='checkbox' checked='checked' value='"+classcodes[j]+"' classes='"+classes[j]+"'><span>"+classes[j]+"班</span></div>";
				}
				$("#exam-classcode").html(classOpt);
			});
		}else{
			
		}
		getAllCourseSubject();
	},null,null);
}

function getAllCourseSubject(){
	var userKey = $("#user-name",parent.parent.document).attr("key");	
	var schoolcode = $("#user-name",parent.parent.document).attr("schoolcode");
	var type = $("#exam-type option:selected").val();
	var data = {
			cmd:"getAllCourseSubject",
			model:"course",
			userKey:userKey,
			data:{
				type:type,
				schoolcode:schoolcode
			},
			remark:"",
			
	};
	AjaxCrud.get(serverUrl.ServerLoginUrl,data,function(res){
		if(res.result == 1){
			var data = res.data;
			Subject = res.data;
			var cont = "<option value='' coursename=''>科目</option>";
			for(var i = 0; i < data.length; i++){
				cont += "<option value='"+data[i].subjectcode+"' coursename='"+data[i].coursecontent+"'>"+data[i].coursecontent+"</option>";
			}
			$("#exam-coursename0").html(cont);
		}
	},null,null);
}
var index = 1;
function pageBtns(){
	check("exam-examname",isNull,"null");
	check("exam-describe",isNull,"null");
	$("#save-btns").click(function(){
		if(checkSave()){
			if(check2()){
				addExam();
			}
		}
	});
//	var courseCodes = [];
//	var isRepeated;
	$(document).on("click",".btn-add",function(){
//		courseCodes = [];
//		$(".exam-coursename option:selected").each(function(){
//			if(this.value != "" && courseCodes.indexOf(this.value) == -1){
//				courseCodes.push(this.value);
//			}
//		});
//		if(courseCodes.length < Subject.length){
			var cont = "<div class='add-info-edit info-edit"+index+"' index='"+index+"'>" +
			"<input type='text' class='Wdate exam-stime' readonly='readonly' id='exam-stime"+index+"' index='"+index+"'/>" +
			"<input type='text' class='Wdate exam-etime' readonly='readonly' id='exam-etime"+index+"' index='"+index+"'/>" +
			"<select class='exam-coursename' id='exam-coursename"+index+"' index='"+index+"'><option value='' coursename=''>科目</option></select>" +
			"<input type='text' placeholder='满分值' style='width:100px;'>" +
			"<div class='btn-cont btn-add btn-edit"+index+"' index='"+index+"'>+</div>" +
			"<span class='info-error' id='info-error"+index+"' index='"+index+"'></span>" +
			"</div>";
			$("#add-info-cont").append(cont);
			
			var k = $(this).attr("index");
			$(".btn-edit" + k).removeClass("btn-add").addClass("btn-del");
			$(".btn-edit" + k).html("-");
			
			var cont = "<option value='' coursename=''>科目</option>";
//			for(var i = 0; i < Subject.length; i++){
//				isRepeated = false;
//				for(var j = 0; j < courseCodes.length; j++){
//					if(courseCodes[j] == Subject[i].subjectcode){
//						isRepeated = true;
//						break;
//					}
//				}
//				if(!isRepeated){
//					cont += "<option value='"+Subject[i].subjectcode+"' coursename='"+Subject[i].coursecontent+"'>"+Subject[i].coursecontent+"</option>";
//				}
//			}
			for(var i = 0; i < Subject.length; i++){
				cont += "<option value='"+Subject[i].subjectcode+"' coursename='"+Subject[i].coursecontent+"'>"+Subject[i].coursecontent+"</option>";
			}
			$("#exam-coursename" + index).html(cont);
			
			index++;
			
//		}else{
//			processError("考试科目已经全部被选择！");
//		}
		
	});
	$(document).on('click','.btn-del', function() {
		var index = $(this).attr("index");
		$(".info-edit"+index).remove();
	});
	$(document).on('click','.exam-stime', function() {
		var index = $(this).attr("index");
		var date = $("#exam-etime" + index).val() + ":00";
		if($("#exam-etime" + index).val() == ""){
			WdatePicker({dateFmt:'yyyy-MM-dd HH:mm'});
		}else{
			var dates = getFormatDateByLong(strToDate(date) - 6*60*60*1000,"yyyy-MM-dd hh:mm:ss");
			WdatePicker({minDate:dates,maxDate:date,dateFmt:'yyyy-MM-dd HH:mm'});
		}
	});
	$(document).on('click','.exam-etime', function() {
		var index = $(this).attr("index");
		var date = $("#exam-stime" + index).val() + ":00";
		if($("#exam-stime" + index).val() == ""){
			WdatePicker({minDate:date,dateFmt:'yyyy-MM-dd HH:mm'});
		}else{
			var dates = getFormatDateByLong(strToDate(date) + 6*60*60*1000,"yyyy-MM-dd hh:mm:ss");
			WdatePicker({minDate:date,maxDate:dates,dateFmt:'yyyy-MM-dd HH:mm'});
		}
	});
	
}

function addExam(){
	var userKey = $("#user-name",parent.parent.document).attr("key");
	var rolecode = $("#user-name",parent.parent.document).attr("rolecodes");
	var schoolcode = $("#user-name",parent.parent.document).attr("schoolcode");
	var classcodeArr = [];
	$(".classcode-item input:checked").each(function(){
		classcodeArr.push(this.value);
	});
	var list = [];
	$(".add-info-edit").each(function(i){
		var param = {};
		param.rolecode = rolecode;
		param.schoolcode = schoolcode;
		param.examname = encodeURI($("#exam-examname").val());
		param.schooltype = $("#exam-type option:selected").val();
		param.grades = $("#exam-grade option:selected").val();
		param.classcode = classcodeArr.join(","); 
		param.describe = encodeURI($("#exam-describe").val());
		$(this).children().each(function(j){
			if(j == 0){
				param["stime"] = strToDate(this.value + ":00")/1000;
				return true;
			}else if(j == 1){
				param["etime"] = strToDate(this.value + ":00")/1000;
				return true;
			}else if(j == 2){
				param["coursecode"] = this.value;
				param["coursename"] = encodeURI($(this).find("option:selected").attr("coursename"));
				return true;
			}else if(j == 3){
				param["score"] = this.value;
				return true;
			}
			
		});
		list.push(param);
	});
	var data = {
			cmd:"addBigExamInfo",
			model:"bigexam",
			userKey:userKey,
			data:JSON.stringify(list),
			remark:"",						
	};
	processBegin("正在保存中···");
	AjaxCrud.add(serverUrl.ServerLoginUrl,data,function(res){
		if(res.result == 1 && res.data != undefined && res.data != ""){
			processEnd("保存成功");
		}else{
			processError("添加考试失败，该考试已存在");
		}
	},null,null);
}

function check2(){
	var flg = false;
	var checkFails = $(".check-fail");
	for(var i = 0; i < checkFails.length; i++){
		if(checkFails[i].innerHTML != ""){
			flg = false;
			break;
		}else{
			flg = true;
		}
	}
	var checkCont = $(".info-error");
	for(var i = 0; i < checkCont.length; i++){
		if(checkCont[i].innerHTML != ""){
			flg = false;
			break;
		}else{
			flg = true;
		}
	}
	return flg;
}

function checkSave(){
	var flag = false;
	var parame = ["examname","classcode","describe","stime","etime","coursecode","score"];
	for(key in parame){
		if(key == "1"){
			if($(".classcode-item input:checked").length == 0){
				$("#exam-classcode").focus();
				$("#exam-classcode-ex").text("不能为空！");
				return false;
			}else{
				$("#exam-classcode-ex").text("");
				flag = true;
			};
		}else if(key == "3" || key == "4" || key == "5" || key == "6" ){
			$(".add-info-edit").each(function(){
				var index = $(this).attr("index");
				$(this).children().each(function(){
					if(this.value == ""){
						$("#info-error" + index).html("不能为空！");
						return false;
					}else{
						$("#info-error" + index).html("");
						flag = true;
					};
				});
			});
		}else{
			if($("#exam-" + parame[key]).val() == ""){
				$("#exam-" + parame[key]).focus();
				$("#exam-" + parame[key] + "-ex").text("不能为空！");
				return false;
			}else{
				flag = true;
			};
		};
	}
	return flag;
}

function initSmPages(){
	var rcH = $(".body-right",parent.document).height() - $(".right-title").height()-3;
	$(".right-content").height(rcH);
	
	var cdsH = $(".right-content").height() - $(".content-title").height() - 40;
	$(".content-data-show").height(cdsH);
}